<?php

class VipController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;


	public function init() {

		$this->commonAction();	

	}
	     
  //处理增加账号信息	
	public function addopAction() {
		try {
			 //处理学生信息
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			 // print_r($p);  exit;
			if(empty($p['realname'])){
				throw new Exception ( '家长姓名不能为空！' );				
				} 
			if(empty($p['mobile'])){
				throw new Exception ( '手机号不能为空！' );				
				}
			if(!preg_match('|^[0-9]{11}$|',$p['mobile'])){
					throw new Exception ('手机号输入错误');
			   } 
			 
			if(empty($p['cnname'])){
				throw new Exception ( '学生姓名不能为空，请确认！' );				
				}else{
				foreach($p['cnname'] as  $key => $value){
					
					if(empty($value)){
						throw new Exception ( '学生姓名不能a为空！' );	
					 } 
				  }	
			  } 
				
			if(empty($p['birthday'])){
				throw new Exception ( '请选择生日！' );				
				}else{
					foreach($p['birthday'] as  $key => $value){
					if(empty($value)){
						throw new Exception ( '生日不能为空！' );	
					 } 
				  }
			   }	
			   
			$isok=0;
		  //家长信息
		  
		  $sql="select * from parents where mobile='".$p['mobile']."'";
		  $parentOne=$this->_dbAdapter ->fetchRow ($sql);
		  
		  if($parentOne){
			$lastid= $parentOne['pid'];
		  }else{			  
			   $data=array();
			   $data['realname'] =$p['realname'];
			   $data['mobile'] =$p['mobile'];
			   $data['username'] =$p['username'];
			   $data['password'] =$this->authcode($p['password'],'','mactop');
			   //查询表中是否添加过此家长 
				
			   $parents=$this->_dbAdapter ->insert ('parents',$data); 
			   $lastid=  $this->_dbAdapter->lastInsertId ();   
		   }
		   if($lastid){
			   $isok=1;
		   }else{
			$this->feedback ( '添加家长失败', '注意', 'javascript:window.history.back();', 'warning' );     
			   } 
		  if($isok)	{
			  $datastudent= array();
			  foreach($p['cnname'] as $key =>$value){
				   $datastudent['cnname']= $value;
				   $datastudent['nikename']= $value;
				   $datastudent['birthday']=$p['birthday'][$key];  
				   $datastudent['sex']=$p['sex'.$key];
				   $datastudent['pid']=$lastid;  
				  $parents=$this->_dbAdapter ->insert ('student',$datastudent);  
				  } 
		  if(!empty($p['id'])){
			  
			 $this->feedback ( '添加学生成功', '成功', '/'.$this->controller.'/haveparents/id/'.$p['id'].'/page/'.$p['page'], 'tip' );     
			}else{ 
			$this->feedback ( '添加学生成功', '成功', 'javascript:window.history.back();', 'tip' );  	   
		   }
		} 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	} 
	//查询学生列表	
	 public function indexAction() {
		try {  	
				 
		    $where='where  1=1';	
			 
		   $groupid = ( int ) $this->_request->getParam ( 'groups' );
		   $school = $this->_request->getParam ( 'school' ); 		
	       $classid  = $this->_request->getParam ( 'class' ); 
		   $cnname  = $this->_request->getParam ( 'cnname' ); 
		   $type  = $this->_request->getParam ( 'type' ); 
		    
		  $this->view->groups=$groupid; 
		  $this->view->school=$school;  
		  $this->view->classid=$classid; 
		  $this->view->cnname=$cnname; 
		   $this->view->type=$type; 
		   $time=time();       
				  if($type==2){
					   //表示 付钱的vip用户
					$where.=' and viptype=2';   
					  }else if($type==1){
						//试用vip，并且时间在试用中	
					   $where.=' and viptype=1 && '.$time.' < endtime';   		
					}else if($type==3){
					//免费用户   ，或者试用vip已经过期							
					 $where.=' and (viptype=1 && '.$time.' > endtime) ||  (viptype=0)';   
						}else{
						$where.='';	
							}   
		  
				  if($school){ 
					  $schoolarray=explode('_',$school);  
					  $schoolid=$schoolarray['0'];
				  }
				   
				 if($groupid){
					  $where .=' and  groupid="'.$groupid.'" ';
					 $this->view->groupid= $groupid; 
					 }
					 
				 if($schoolid){
					$where .=' and  schoolid="'.$schoolid.'" '; 
					$this->view->schoolid= $schoolid; 
					} 
					
				 if($classid){
					$where .=' and  classid="'.$classid.'" ';
					  $this->view->classid= $classid; 
					 }	
					 
				 if($cnname){
					$where.=' and cnname="'.$cnname.'" ';
					$this->view->cnname= $cnname; 				 
					} 
			  
			$count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			    $sql="select SQL_CALC_FOUND_ROWS *  from student_unit_v ".$where."  order by pid ASC limit $offset,$count";
			    
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			 //  print_r($result);
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		 	$page->setvar ( array ('groups' => $groupid,'school'=>$school,'class'=>$classid,'cnname'=>$cnname,'type'=>$type) );
				$this->view->page= $page->getNumPage ();
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	} 
   public function groupsAction() {
		try { 
         $sql="select * from  groups ";
	     $result = $this->_dbAdapter->fetchAll ($sql);
		 for($i = 0; $i<count($result); $i++)
		{
	      $str .= "<option value='".$result[$i]['groupid']."'>".$result[$i]['groupname']."</option>";
		}
		  
	    echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//获取所有的校园列表
	  public function schoollistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost ();  
		 
		  $sql="SELECT * FROM `groups` where groupid='".$p['id']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
	 
/*			//连接运营库
			$dbArray=array();
			$dbArray['host']=$result['dbhost'];
			$dbArray['username']=$result['dbusername'];
			$dbArray['password']=$this->authcode($result['dbpassword'], 'DECODE', 'mactop') ;
			$dbArray['dbname']=$result['dbname'];
			
			$dbAdapter = Zend_Db::factory ("PDO_MYSQL", $dbArray );
			Zend_Registry::set('dbAdapter', $dbAdapter);

			$dbAdapter=Zend_Registry::get('dbAdapter');
			$dbAdapter->query ( 'set names utf8');	
*/			 
		    $sql="select * from  school where groupid='".$p['id']."'";   
			$schoollist= $this->_dbAdapter->fetchAll ($sql);
	                 

			 
		 for($i = 0; $i<count($schoollist); $i++)
		{
		  $str .= "<option value='".$schoollist[$i]['schoolid'].'_'.$result['groupid']."'>".$schoollist[$i]['schoolname']."</option>";
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	
	//获取所有的班级信息
	  public function classlistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost ();  
		  $array=explode('_',$p['id']);
		  
		  $sql="SELECT * FROM `groups` where groupid='".$array['1']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
	 
/*			//连接运营库
			$dbArray=array();
			$dbArray['host']=$result['dbhost'];
			$dbArray['username']=$result['dbusername'];
			$dbArray['password']=$this->authcode($result['dbpassword'], 'DECODE', 'mactop') ;
			$dbArray['dbname']=$result['dbname'];
			
			$dbAdapter = Zend_Db::factory ("PDO_MYSQL", $dbArray );
			Zend_Registry::set('dbAdapter', $dbAdapter);

			$dbAdapter=Zend_Registry::get('dbAdapter');
			$dbAdapter->query ( 'set names utf8');	
*/			 
		  $sql="select * from  class  where isdel=0 and schoolid='".$array['0']."' ";   
		  $classlist= $this->_dbAdapter->fetchAll ($sql);
	  
			 
		 for($i = 0; $i<count($classlist); $i++)
		{
		  $str .= "<option value='".$classlist[$i]['classid']."'>".$classlist[$i]['classname']."</option>";
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//vip 服务设置
	
	public function serviceAction() {
	 try{
	  //查询所有的待处理订单 ispay=0 
	  		 
		    $where='where  1=1';	
			 
		   $groupid = ( int ) $this->_request->getParam ( 'groups' );
		   $school = $this->_request->getParam ( 'school' ); 		
	       $classid  = $this->_request->getParam ( 'class' ); 
		   $cnname  = $this->_request->getParam ( 'cnname' ); 
		   $type  = $this->_request->getParam ( 'type' ); 
		    
		  $this->view->groups=$groupid; 
		  $this->view->school=$school;  
		  $this->view->classid=$classid; 
		  $this->view->cnname=$cnname; 
		  $this->view->type=$type; 
		     
			 $where=' where 1=1 ';
			 if($type==4){
			   //待处理订单
				$where.=' and  ispay=0'; 
				$table='student_order_v';
			   }else if($type==1){ 
				  $where.='and ispay=1 or ispay=2';  
				  $table='student_order_v';  
				}else if($type==2){
					 $where.='and ispay=2'; 
					 $table='order_pay_v';
				}else if($type==3){
					 $where.='and ispay=1';   
					 $table='student_order_v';   
				}else if($type==5){
					 $where.='and isdelete=1';   
					 $table='student_order_v';   
				}else{
				   $table='student_order_v';   	
					} 
			    
				 if($school){ 
					  $schoolarray=explode('_',$school);  
					  $schoolid=$schoolarray['0'];
				  } 
				  
				 if($groupid){
					  $where .=' and  groupid="'.$groupid.'" ';
					 $this->view->groupid= $groupid; 
					 }
					 
				 if($schoolid){
					$where .=' and  schoolid="'.$schoolid.'" '; 
					$this->view->schoolid= $schoolid; 
					} 
					
				 if($classid){
					$where .=' and  classid="'.$classid.'" ';
					  $this->view->classid= $classid; 
					 }	
					 
				 if($cnname){
					$where.=' and cnname="'.$cnname.'" ';
					$this->view->cnname= $cnname; 				 
					}  
				  
			  
			$count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			    $sql='select SQL_CALC_FOUND_ROWS *  from  '.$table.' '.$where.' order by pid ASC limit '.$offset.','.$count;
			     
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			   
		    	$this->view->result= $result;
			     //print_r($result);
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		 	$page->setvar ( array ('groups' => $groupid,'school'=>$school,'class'=>$classid,'cnname'=>$cnname,'type'=>$type) );
				$this->view->page= $page->getNumPage ();
	  		
			
	
	  } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//开通vip
	public function openvipAction() {
	 try{
	  //更新 payorder表中的 vipetype  为1  并且 把student中的viptype  为 2
	    $payorderid  = $this->_request->getParam ( 'orderid' ); 
	 	$uid  = $this->_request->getParam ( 'uid' ); 
    	$type  = $this->_request->getParam ( 'type' ); 
   
		if(empty($payorderid)){
			throw new Exception ( '参数错误！' );
			}
		if(empty($uid)){
			throw new Exception ( '参数错误！' );
			}	
		$isok=0;
			
	    $updateorder=$this->_dbAdapter ->update ( 'payorder',array('ispay'=>1),'payorderid='.$payorderid.' and student_uid='.$uid ); 
	    if($updateorder){
			$studentvip=$this->_dbAdapter ->update ( 'student',array('viptype'=>2),'uid='.$uid ); 
			if($studentvip){
				$isok=1;
				} else{
				  $isok=0;	
					}
		 }else{
			$isok=0; 
			 }
		
	   if($isok>0){
			$this->feedback ( '开通成功', '注意', '/'.$this->controller.'/service/type/'.$type, 'tip' );
	       }else{	
			 $this->feedback ( '开通失败', 'note', 'javascript:window.history.back();', 'warning' );
	       }
	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//付费
	public function payAction() {
	 try{
	   $payorderid  = $this->_request->getParam ( 'orderid' ); 
	   $price  = $this->_request->getParam ( 'price' );  
	   $type  = $this->_request->getParam ( 'type' );  
	   if(empty($payorderid)){
			throw new Exception ( '参数错误！' );
			}
		if(empty($price)){
			throw new Exception ( '参数错误！' );
			}	
		$isok=0;
			
	    $updateorder=$this->_dbAdapter ->update ( 'payorder',array('ispay'=>2),'payorderid='.$payorderid ); 
	    if($updateorder){
			$data=array();
			$time=time();
			$data['payorderid']=$payorderid;
			$data['paymoney']=$price;
			$data['paytype']='2';
			$data['paytime']=$time;
			$payvip=$this->_dbAdapter ->insert ('pay',$data ); 
			if($payvip){
				$isok=1;
				} else{
				  $isok=0;	
					}
		 }else{
			$isok=0; 
			 }
		
	   if($isok>0){
			$this->feedback ( '恭喜付费成功！', '注意', '/'.$this->controller.'/service/type/'.$type, 'tip' );
	       }else{	
			 $this->feedback ( '操作失败', 'note', 'javascript:window.history.back();', 'warning' );
	       }
	    
	
	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//作废订单	
	public function deleteAction() {
	 try{
	   $payorderid  = $this->_request->getParam ( 'orderid' );  
	   $type  = $this->_request->getParam ( 'type' );  
	   if(empty($payorderid)){
			throw new Exception ( '参数错误！' );
			} 
	    $updateorder=$this->_dbAdapter ->update ( 'payorder',array('isdelete'=>1),'payorderid='.$payorderid ); 
	    
		if($updateorder){
			$this->feedback ( '该订单已作废！', '注意', '/'.$this->controller.'/service/type/'.$type, 'tip' ); 
		 }else{
		 	$this->feedback ( '操作失败', 'note', 'javascript:window.history.back();', 'warning' );
			 } 
	
	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	 
	
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

